﻿#define DEBUG

using System;
using System.Collections.Generic;
using System.Text;

using System.Collections;
using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm.MakingBuffer
{
    class YDGoodsNDOosaki : Common, IRoute 
    {

        public YDGoodsNDOosaki(soDatasets objDSs, Hashtable routeLnWithBfr, soStrings objStrFields)
            : base(objDSs, routeLnWithBfr, objStrFields)
        {
        }

        #region IRoute メンバ

        public override void MakeBufferForRoute(soDatasetVector objVector)
        {
            soRecordset objRS = null;
            soRecordset objSubRS = null;

            objRS = objVector.Query(
                //山手貨物線
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0102' and " +
                RouteDataset.FLD_DAIBUN_CD + " = '01' and " +
                RouteDataset.FLD_TSUUSHO_CD + " in ('465')) or " +
                //大崎支線
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0102' and " +
                RouteDataset.FLD_DAIBUN_CD + " = '01' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '130') ", 
                false, objStrFields,
                "group by " + RouteDataset.FLD_FEATURE_CD + ", " + RouteDataset.FLD_DAIBUN_CD + ", " + RouteDataset.FLD_TSUUSHO_CD);

            if (objRS != null && objRS.RecordCount > 0)
            {
                if (!objRS.IsBOF())
                {
                    objRS.MoveFirst();
                }
#if DEBUG
                for (int j = 0; j < objRS.RecordCount; objRS.MoveNext(), j++)
#else
                for (; !objRS.IsEOF(); objRS.MoveNext())
#endif
                {
                    try
                    {
                        feature = objRS.GetFieldValue(RouteDataset.FLD_FEATURE_CD).ToString();
                        daibun = objRS.GetFieldValue(RouteDataset.FLD_DAIBUN_CD).ToString();
                        tsuusho = objRS.GetFieldValue(RouteDataset.FLD_TSUUSHO_CD).ToString();

                        Console.WriteLine(
                            String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}",
                                RouteDataset.FLD_FEATURE_CD, feature,
                                RouteDataset.FLD_DAIBUN_CD, daibun,
                                RouteDataset.FLD_TSUUSHO_CD, tsuusho,
                                j + 1, objRS.RecordCount));

                        objSubRS = objVector.Query(
                            RouteDataset.FLD_FEATURE_CD + " = '" + feature + "' and " +
                            RouteDataset.FLD_DAIBUN_CD + " = '" + daibun + "' and " +
                            RouteDataset.FLD_TSUUSHO_CD + " = '" + tsuusho + "'",
                            true, null, "");

                        int multiLineCount = objSubRS.RecordCount;
                        if (objSubRS != null && multiLineCount > 0)
                        {
                            if (!objSubRS.IsBOF())
                            {
                                objSubRS.MoveFirst();
                            }

                            soGeoLine objLineTmp = null;
                            soGeoLine[] objMultiLines = new soGeoLine[Utility.MAX_MULTIPLE_LINES]; //複数非連続線路
                            soDatasetVector objDSVKirotei = objDSs["キロ標_東京"] as soDatasetVector;
                            int i = 0;
                            for (; !objSubRS.IsEOF(); objSubRS.MoveNext())
                            {
                                soRecordset objRSContain = null;
                                objRSContain = objDSVKirotei.QueryEx(objSubRS.GetGeometry(), seSpatialQueryMode.scsCommonPoint, "");
                                objLineTmp = objSubRS.GetGeometry() as soGeoLine;
                                if (objRSContain == null || objLineTmp.Length < 100)
                                {
                                    continue;
                                }
                                objMultiLines[i++] = objLineTmp;
                                Marshal.ReleaseComObject(objRSContain);
                                objRSContain = null;
                            }
                            objSubRS.Close();
                            Marshal.ReleaseComObject(objDSVKirotei);
                            objDSVKirotei = null;

                            //routeInfo = new RouteArea(objDSs["キロ標_東京"]);
                            //routeInfo.SetInfo(feature, daibun, tsuusho);

                            //複数非連続線路
                            for (int k = 0; k < i; k++)
                            {
                                Console.WriteLine(
                                    String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}---\t{1},{3},{5}",
                                        RouteDataset.FLD_FEATURE_CD, feature, RouteDataset.FLD_DAIBUN_CD, daibun, RouteDataset.FLD_TSUUSHO_CD, tsuusho,
                                        k + 1, multiLineCount));

                                //MakeBufferForPart(objMultiLines[k]);
                            }

                            Marshal.ReleaseComObject(objLineTmp);
                            objLineTmp = null;
                        }
                    }
                    catch (System.Threading.ThreadAbortException ae)
                    {
                        //System.Windows.Forms.MessageBox.Show("アプリケーションが中止された！", ae.Message);
                        throw ae;
                    }
                    catch (Exception ex)
                    {
                        objSubRS.Close();
                        Marshal.ReleaseComObject(objSubRS);
                        objSubRS = null;

                        throw new Exception(ex.Message);
                    }
                }

                objSubRS.Close();
                Marshal.ReleaseComObject(objSubRS);
                objSubRS = null;
                objRS.Close();
                Marshal.ReleaseComObject(objRS);
                objRS = null;
            }

            if (objRS != null)
            {
                objRS.Close();
                Marshal.ReleaseComObject(objRS);
                objRS = null;
            }
        }

        #endregion
    }
}
